package com.blunka.framework.client.db.druid.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.blunka.framework.client.constants.InitDesignConfig;
import com.blunka.framework.client.db.druid.dbutils.IServiceDbHelper;
import com.blunka.framework.client.db.druid.dbutils.ServerDbHelper;
import com.blunka.framework.client.db.entity.BatchSqlEntity;
import com.blunka.framework.client.enums.ModelType;
import com.blunka.framework.client.ui.entity.BlkTableMetaData;
import com.blunka.framework.client.utils.DesignUtils;

/**
 *
 *
 * @平台：统一开发平台
 * @文件名称：SqlLiteServerDbHelper.java
 * @文件描述：SqlLite数据库
 * @author xuqiang
 * @日期：2018年5月25日下午4:56:13
 *
 */
public class SqlLiteServer implements ISQLService{
	private static final Logger logger = Logger.getLogger(SqlLiteServer.class);

	/**
	 * 本地sqllite数据库
	 */
	public static IServiceDbHelper serviceDbHelper;

	private static SqlLiteServer dbHelper = new SqlLiteServer();

	public static SqlLiteServer newInstance(){
		return dbHelper;
	}

	private SqlLiteServer(){
		try{
			Properties p = new Properties();
			p.put("driverClassName", "org.sqlite.JDBC");
			p.put("url", "jdbc:sqlite:"+DesignUtils.getDir()+"/res/db/thumnp.db");
			p.put("username", "");
			p.put("password", "");
			p.put("maxActive", "25");
			p.put("initialSize", "1");
			p.put("maxWait", "60000");
			p.put("maxIdle", "25");
			p.put("minIdle", "1");
			p.put("timeBetweenEvictionRunsMillis", "60000");
			p.put("minEvictableIdleTimeMillis", "300000");
			p.put("filters", "stat");

			serviceDbHelper = new ServerDbHelper();
			serviceDbHelper.initDataSource(p);
		}catch(Exception e){
			logger.error("数据库连接失败",e);
		}
	}

	/**
	 * 获取业务建模数据版本
	 * @return
	 */
	public String getDBVersion(){
		Map<String,String> versionMap = dbHelper.getDBUtils().getItemMap("SELECT * FROM BLK_TABLE_VERSION");
		if(versionMap == null) {
			return "VERSION.1.0";
		}
		return versionMap.get("DB_VERSION");
	}

	/**
	 * 获取数据库地址
	 * @return
	 */
	public String getDBServerIP(){
		return "127.0.0.1";
	}
	
	/**
	 * 获取数据库名称
	 * @return
	 */
	public String getDBServerName() {
		return null;
	}

	/**
	 * 获取数据库操作工具
	 * @return
	 */
	public IServiceDbHelper getDBUtils(){
		return serviceDbHelper;
	}

	/**
	 * 初始化远程数据库
	 * @throws Exception
	 */
	public void initServerDB()throws Exception{
		List<String> sqlList = InitDesignConfig.getServerDBSql();

		List<BatchSqlEntity> executeSql = new ArrayList<BatchSqlEntity>();
		for(String sql : sqlList){
			BatchSqlEntity sqlEntity = new BatchSqlEntity();
			sqlEntity.setSql(sql);

			executeSql.add(sqlEntity);
		}
		dbHelper.getDBUtils().batch(executeSql);
	}

	/**
	 * 查询数据结合
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<BlkTableMetaData> queryAllNode() throws Exception {
		List<BlkTableMetaData> list = dbHelper.getDBUtils().getBeanList("SELECT * FROM BLK_TABLE_METADATA WHERE MODEL_STATES !='4' ORDER BY ORDER_BY ASC", BlkTableMetaData.class);
		return list;
	}

	/**
	 * 插入新节点，并且返回插入的节点内容
	 * @param node
	 * @return
	 */
	@Override
	public BlkTableMetaData insertNode(BlkTableMetaData node) throws Exception {
		String tablemetadata_id = DesignUtils.getRandom();
		StringBuffer sql = new StringBuffer("INSERT INTO BLK_TABLE_METADATA(TABLEMETADATA_ID,FATHER_TABLEMETADATA_ID,ZH_CN,EN_US,DESCRIBES,OBJ_PATH,MODEL_STATES,MODEL_TYPE,VIEW_SQL,ORDER_BY) VALUES(?,?,?,?,?,?,?,?,?,?)");
		Object params[] = {tablemetadata_id,
				node.getFather_tablemetadata_id(),
				node.getZh_cn(),
				node.getEn_us(),
				node.getDescribes(),
				node.getObj_path(),
				node.getModel_states().getIndex(),
				node.getModel_type().getIndex(),
				node.getView_sql(),
				node.getOrder_by()
				};
		dbHelper.getDBUtils().execute(sql.toString(), params);
		//获取
		BlkTableMetaData newNode = dbHelper.getDBUtils().getBean("SELECT * FROM BLK_TABLE_METADATA WHERE TABLEMETADATA_ID='"+tablemetadata_id+"'", BlkTableMetaData.class);
		return newNode;
	}
	
	/**
	 * 插入新节点，并且返回插入的节点内容
	 * @param node
	 * @return
	 */
	@Override
	public BlkTableMetaData insertNodeNoNewId(BlkTableMetaData node)throws Exception{
		String tablemetadata_id = node.getTablemetadata_id();
		StringBuffer sql = new StringBuffer("INSERT INTO BLK_TABLE_METADATA(TABLEMETADATA_ID,FATHER_TABLEMETADATA_ID,ZH_CN,EN_US,DESCRIBES,OBJ_PATH,MODEL_STATES,MODEL_TYPE,VIEW_SQL,ORDER_BY) VALUES(?,?,?,?,?,?,?,?,?,?)");
		Object params[] = {tablemetadata_id,
				node.getFather_tablemetadata_id(),
				node.getZh_cn(),
				node.getEn_us(),
				node.getDescribes(),
				node.getObj_path(),
				node.getModel_states().getIndex(),
				node.getModel_type().getIndex(),
				node.getView_sql(),
				node.getOrder_by()
				};
		dbHelper.getDBUtils().execute(sql.toString(), params);
		//获取
		BlkTableMetaData newNode = dbHelper.getDBUtils().getBean("SELECT * FROM BLK_TABLE_METADATA WHERE TABLEMETADATA_ID='"+tablemetadata_id+"'", BlkTableMetaData.class);
		return newNode;
	}
	
	/**
	 * 获取插入新节点sql
	 * @param node
	 * @return
	 * @throws Exception
	 */
	public BatchSqlEntity getinsertNodeNoNewIdSql(BlkTableMetaData node) throws Exception {
		String tablemetadata_id = node.getTablemetadata_id();
		StringBuffer sql = new StringBuffer("INSERT INTO BLK_TABLE_METADATA(TABLEMETADATA_ID,FATHER_TABLEMETADATA_ID,ZH_CN,EN_US,DESCRIBES,OBJ_PATH,MODEL_STATES,MODEL_TYPE,VIEW_SQL,ORDER_BY) VALUES(?,?,?,?,?,?,?,?,?,?)");
		Object params[] = {tablemetadata_id,
				node.getFather_tablemetadata_id(),
				node.getZh_cn(),
				node.getEn_us(),
				node.getDescribes(),
				node.getObj_path(),
				node.getModel_states().getIndex(),
				node.getModel_type().getIndex(),
				node.getView_sql(),
				node.getOrder_by()
				};
		
		BatchSqlEntity entity = new BatchSqlEntity();
		entity.setSql(sql.toString());
		entity.setParams(params);
		
		return entity;
	}
	
	/**
	 * 新建表，只存储业务模型数据
	 * @param node
	 * @return
	 * @throws Exception
	 */
	public BlkTableMetaData insertTableNodeModel(BlkTableMetaData node) throws Exception{
		String tablemetadata_id = node.getTablemetadata_id();
		StringBuffer sql = new StringBuffer("INSERT INTO BLK_TABLE_METADATA(TABLEMETADATA_ID,FATHER_TABLEMETADATA_ID,ZH_CN,EN_US,DESCRIBES,OBJ_PATH,MODEL_STATES,MODEL_TYPE,VIEW_SQL,ORDER_BY) VALUES(?,?,?,?,?,?,?,?,?,?)");
		Object params[] = {tablemetadata_id,
				node.getFather_tablemetadata_id(),
				node.getZh_cn(),
				node.getEn_us(),
				node.getDescribes(),
				node.getObj_path(),
				node.getModel_states().getIndex(),
				node.getModel_type().getIndex(),
				node.getView_sql(),
				node.getOrder_by()
				};
		dbHelper.getDBUtils().execute(sql.toString(), params);
		//获取
		BlkTableMetaData newNode = dbHelper.getDBUtils().getBean("SELECT * FROM BLK_TABLE_METADATA WHERE TABLEMETADATA_ID='"+tablemetadata_id+"'", BlkTableMetaData.class);
		return newNode;
	}
	
	/**
	 * 新建表
	 * @param node
	 * @return
	 * @throws Exception
	 */
	@Override
	public BlkTableMetaData insertTableNodeNoNewId(BlkTableMetaData node)throws Exception{
		String tablemetadata_id = node.getTablemetadata_id();
		StringBuffer sql = new StringBuffer("INSERT INTO BLK_TABLE_METADATA(TABLEMETADATA_ID,FATHER_TABLEMETADATA_ID,ZH_CN,EN_US,DESCRIBES,OBJ_PATH,MODEL_STATES,MODEL_TYPE,VIEW_SQL,ORDER_BY) VALUES(?,?,?,?,?,?,?,?,?,?)");
		Object params[] = {tablemetadata_id,
				node.getFather_tablemetadata_id(),
				node.getZh_cn(),
				node.getEn_us(),
				node.getDescribes(),
				node.getObj_path(),
				node.getModel_states().getIndex(),
				node.getModel_type().getIndex(),
				node.getView_sql(),
				node.getOrder_by()
				};
		dbHelper.getDBUtils().execute(sql.toString(), params);
		//获取
		BlkTableMetaData newNode = dbHelper.getDBUtils().getBean("SELECT * FROM BLK_TABLE_METADATA WHERE TABLEMETADATA_ID='"+tablemetadata_id+"'", BlkTableMetaData.class);
		return newNode;
	}

	/**
	 * 更新节点
	 * @param node
	 * @return
	 * @throws Exception
	 */
	public BlkTableMetaData updateNode(BlkTableMetaData node) throws Exception{
		List<BatchSqlEntity> executeSql = new ArrayList<BatchSqlEntity>();
		StringBuffer sql = new StringBuffer("update BLK_TABLE_METADATA SET FATHER_TABLEMETADATA_ID=?,ZH_CN=?,EN_US=?,DESCRIBES=?,MODEL_STATES=?,MODEL_TYPE=?,VIEW_SQL=?,ORDER_BY=? WHERE TABLEMETADATA_ID=?");
		Object params[] = {
				node.getFather_tablemetadata_id(),
				node.getZh_cn(),
				node.getEn_us(),
				node.getDescribes(),
				node.getObj_path(),
				node.getModel_states().getIndex(),
				node.getModel_type().getIndex(),
				node.getView_sql(),
				node.getOrder_by(),
				node.getTablemetadata_id()
				};
		BatchSqlEntity entity = new BatchSqlEntity();
		entity.setSql(sql.toString());
		entity.setParams(params);

		executeSql.add(entity);
		dbHelper.getDBUtils().batch(executeSql);
		return node;
	}

	/**
	 * 删除节点
	 * @param node
	 * @return
	 * @throws Exception
	 */
	public BlkTableMetaData deleteNode(BlkTableMetaData node) throws Exception{

		return node;
	}

	/**
	 * 新建表
	 * @param node
	 * @return
	 * @throws Exception
	 */
	@Override
	public BlkTableMetaData insertTableNode(BlkTableMetaData node) throws Exception {
		List<BatchSqlEntity> executeSql = new ArrayList<BatchSqlEntity>();
		String tablemetadata_id = DesignUtils.getRandom();
		StringBuffer sql = new StringBuffer("INSERT INTO BLK_TABLE_METADATA(TABLEMETADATA_ID,FATHER_TABLEMETADATA_ID,ZH_CN,EN_US,DESCRIBES,OBJ_PATH,MODEL_STATES,MODEL_TYPE,VIEW_SQL,ORDER_BY) VALUES(?,?,?,?,?,?,?,?,?,?)");
		Object params[] = {tablemetadata_id,
				node.getFather_tablemetadata_id(),
				node.getZh_cn(),
				node.getEn_us(),
				node.getDescribes(),
				node.getObj_path(),
				node.getModel_states().getIndex(),
				node.getModel_type().getIndex(),
				node.getView_sql(),
				node.getOrder_by()
				};
		BatchSqlEntity entity = new BatchSqlEntity();
		entity.setSql(sql.toString());
		entity.setParams(params);

		executeSql.add(entity);

		dbHelper.getDBUtils().batch(executeSql);
		//获取
		BlkTableMetaData newNode = dbHelper.getDBUtils().getBean("SELECT * FROM BLK_TABLE_METADATA WHERE TABLEMETADATA_ID='"+tablemetadata_id+"'", BlkTableMetaData.class);
		return newNode;
	}
	
	/**
	 * 获取插入表或视图节点sql
	 * @param node
	 * @return
	 * @throws Exception
	 */
	public BatchSqlEntity getInsertTableNodeSql(BlkTableMetaData node) throws Exception {
		StringBuffer sql = new StringBuffer("INSERT INTO BLK_TABLE_METADATA(TABLEMETADATA_ID,FATHER_TABLEMETADATA_ID,ZH_CN,EN_US,DESCRIBES,OBJ_PATH,MODEL_STATES,MODEL_TYPE,VIEW_SQL,ORDER_BY) VALUES(?,?,?,?,?,?,?,?,?,?)");
		Object params[] = {node.getTablemetadata_id(),
				node.getFather_tablemetadata_id(),
				node.getZh_cn(),
				node.getEn_us(),
				node.getDescribes(),
				node.getObj_path(),
				node.getModel_states().getIndex(),
				node.getModel_type().getIndex(),
				node.getView_sql(),
				node.getOrder_by()
				};
		BatchSqlEntity entity = new BatchSqlEntity();
		entity.setSql(sql.toString());
		entity.setParams(params);
		
		return entity;
	}
	
	/**
	 * 更新表，只更新业务数据
	 * @param node
	 * @return
	 * @throws Exception
	 */
	public BlkTableMetaData updateTableNodeModel(BlkTableMetaData node)throws Exception{
		List<BatchSqlEntity> executeSql = new ArrayList<BatchSqlEntity>();
		StringBuffer sql = new StringBuffer("update BLK_TABLE_METADATA SET FATHER_TABLEMETADATA_ID=?,ZH_CN=?,EN_US=?,DESCRIBES=?,MODEL_STATES=?,MODEL_TYPE=?,VIEW_SQL=?,ORDER_BY=? WHERE TABLEMETADATA_ID=?");
		Object params[] = {
				node.getFather_tablemetadata_id(),
				node.getZh_cn(),
				node.getEn_us(),
				node.getDescribes(),
				node.getObj_path(),
				node.getModel_states().getIndex(),
				node.getModel_type().getIndex(),
				node.getView_sql(),
				node.getOrder_by(),
				node.getTablemetadata_id()
				};
		BatchSqlEntity entity = new BatchSqlEntity();
		entity.setSql(sql.toString());
		entity.setParams(params);

		executeSql.add(entity);

		dbHelper.getDBUtils().batch(executeSql);

		return node;
	}
	

	/**
	 * 更新表
	 * @param node
	 * @return
	 * @throws Exception
	 */
	public BlkTableMetaData updateTableNode(BlkTableMetaData node)throws Exception{
		List<BatchSqlEntity> executeSql = new ArrayList<BatchSqlEntity>();
		StringBuffer sql = new StringBuffer("update BLK_TABLE_METADATA SET FATHER_TABLEMETADATA_ID=?,ZH_CN=?,EN_US=?,DESCRIBES=?,MODEL_STATES=?,MODEL_TYPE=?,VIEW_SQL=?,ORDER_BY=? WHERE TABLEMETADATA_ID=?");
		Object params[] = {
				node.getFather_tablemetadata_id(),
				node.getZh_cn(),
				node.getEn_us(),
				node.getDescribes(),
				node.getObj_path(),
				node.getModel_states().getIndex(),
				node.getModel_type().getIndex(),
				node.getView_sql(),
				node.getOrder_by(),
				node.getTablemetadata_id()
				};
		BatchSqlEntity entity = new BatchSqlEntity();
		entity.setSql(sql.toString());
		entity.setParams(params);

		executeSql.add(entity);

		dbHelper.getDBUtils().batch(executeSql);

		return node;
	}
	
	/**
	 * 删除表,只删除业务数据
	 * @param node
	 * @return
	 * @throws Exception
	 */
	public BlkTableMetaData	deleteTableNodeModel(BlkTableMetaData node)throws Exception{
		return node;
	}

	/**
	 * 删除表
	 * @param node
	 * @return
	 * @throws Exception
	 */
	public BlkTableMetaData	deleteTableNode(BlkTableMetaData node)throws Exception{

		return node;
	}
	
	/**
	 * 插入字段,只插入业务数据
	 * @param node 新节点
	 * @param table 所属表名
	 * @return
	 * @throws Exception
	 */
	@Override
	public BlkTableMetaData insertFieldModel(BlkTableMetaData newBlkModel, BlkTableMetaData parentBlkModel)throws Exception{
		return insertField(newBlkModel,parentBlkModel);
	}

	/**
	 * 插入字段
	 * @param node 新节点
	 * @param table 所属表名
	 * @return
	 * @throws Exception
	 */
	@Override
	public BlkTableMetaData insertField(BlkTableMetaData newBlkModel, BlkTableMetaData parentBlkModel)
			throws Exception {
		List<BatchSqlEntity> executeSql = new ArrayList<BatchSqlEntity>();
		String tablemetadata_id = DesignUtils.getRandom();
		StringBuffer sql = new StringBuffer("INSERT INTO BLK_TABLE_METADATA(TABLEMETADATA_ID,FATHER_TABLEMETADATA_ID,ZH_CN,EN_US,DESCRIBES,OBJ_PATH,MODEL_STATES,MODEL_TYPE,MODEL_FIELD,MODEL_FIELD_LEN,IS_PRIMARY_KEY,IS_EMPTY,SHOW_WIDTH,IS_HIDDEN,IS_VIEWER,IS_READER,IS_DISABLED,ERROR_MSG,INPUT_PROMIT,GROUP_TITLE,USER_CONFIG,SEL_OPTIONS,VIEW_SQL,ORDER_BY) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
		Object params[] = {tablemetadata_id,
				newBlkModel.getFather_tablemetadata_id(),
				newBlkModel.getZh_cn(),
				newBlkModel.getEn_us(),
				newBlkModel.getDescribes(),
				newBlkModel.getObj_path(),
				newBlkModel.getModel_states().getIndex(),
				newBlkModel.getModel_type().getIndex(),
				newBlkModel.getModel_field().getIndex(),
				newBlkModel.getModel_field_len(),
				newBlkModel.getIs_primary_key().getIndex(),
				newBlkModel.getIs_empty().getIndex(),
				newBlkModel.getShow_width(),
				newBlkModel.getIs_hidden().getIndex(),
				newBlkModel.getIs_viewer().getIndex(),
				newBlkModel.getIs_reader().getIndex(),
				newBlkModel.getIs_disabled().getIndex(),
				newBlkModel.getError_msg(),
				newBlkModel.getInput_promit(),
				newBlkModel.getGroup_title(),
				newBlkModel.getUser_config(),
				newBlkModel.getSel_options(),
				newBlkModel.getView_sql(),
				newBlkModel.getOrder_by()
				};
		BatchSqlEntity entity = new BatchSqlEntity();
		entity.setSql(sql.toString());
		entity.setParams(params);

		executeSql.add(entity);

		dbHelper.getDBUtils().batch(executeSql);
		//获取
		BlkTableMetaData newNode = dbHelper.getDBUtils().getBean("SELECT * FROM BLK_TABLE_METADATA WHERE TABLEMETADATA_ID='"+tablemetadata_id+"'", BlkTableMetaData.class);
		return newNode;
	}
	
	/**
	 * 插入字段
	 * @param newBlkModel
	 * @return
	 * @throws Exception
	 */
	@Override
	public BlkTableMetaData insertField(BlkTableMetaData newBlkModel)throws Exception{
		List<BatchSqlEntity> executeSql = new ArrayList<BatchSqlEntity>();
		StringBuffer sql = new StringBuffer("INSERT INTO BLK_TABLE_METADATA(TABLEMETADATA_ID,FATHER_TABLEMETADATA_ID,ZH_CN,EN_US,DESCRIBES,OBJ_PATH,MODEL_STATES,MODEL_TYPE,MODEL_FIELD,MODEL_FIELD_LEN,IS_PRIMARY_KEY,IS_EMPTY,SHOW_WIDTH,IS_HIDDEN,IS_VIEWER,IS_READER,IS_DISABLED,ERROR_MSG,INPUT_PROMIT,GROUP_TITLE,USER_CONFIG,SEL_OPTIONS,VIEW_SQL,ORDER_BY) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
		Object params[] = {newBlkModel.getTablemetadata_id(),
				newBlkModel.getFather_tablemetadata_id(),
				newBlkModel.getZh_cn(),
				newBlkModel.getEn_us(),
				newBlkModel.getDescribes(),
				newBlkModel.getObj_path(),
				newBlkModel.getModel_states().getIndex(),
				newBlkModel.getModel_type().getIndex(),
				newBlkModel.getModel_field().getIndex(),
				newBlkModel.getModel_field_len(),
				newBlkModel.getIs_primary_key().getIndex(),
				newBlkModel.getIs_empty().getIndex(),
				newBlkModel.getShow_width(),
				newBlkModel.getIs_hidden().getIndex(),
				newBlkModel.getIs_viewer().getIndex(),
				newBlkModel.getIs_reader().getIndex(),
				newBlkModel.getIs_disabled().getIndex(),
				newBlkModel.getError_msg(),
				newBlkModel.getInput_promit(),
				newBlkModel.getGroup_title(),
				newBlkModel.getUser_config(),
				newBlkModel.getSel_options(),
				newBlkModel.getView_sql(),
				newBlkModel.getOrder_by()
				};
		BatchSqlEntity entity = new BatchSqlEntity();
		entity.setSql(sql.toString());
		entity.setParams(params);

		executeSql.add(entity);

		dbHelper.getDBUtils().batch(executeSql);
		//获取
		BlkTableMetaData newNode = dbHelper.getDBUtils().getBean("SELECT * FROM BLK_TABLE_METADATA WHERE TABLEMETADATA_ID='"+newBlkModel.getTablemetadata_id()+"'", BlkTableMetaData.class);
		return newNode;
	}
	
	/**
	 * 获取插入字段sql
	 * @param newBlkModel
	 * @return
	 * @throws Exception
	 */
	public BatchSqlEntity getInsertFieldSql(BlkTableMetaData newBlkModel)throws Exception{
		StringBuffer sql = new StringBuffer("INSERT INTO BLK_TABLE_METADATA(TABLEMETADATA_ID,FATHER_TABLEMETADATA_ID,ZH_CN,EN_US,DESCRIBES,OBJ_PATH,MODEL_STATES,MODEL_TYPE,MODEL_FIELD,MODEL_FIELD_LEN,IS_PRIMARY_KEY,IS_EMPTY,SHOW_WIDTH,IS_HIDDEN,IS_VIEWER,IS_READER,IS_DISABLED,ERROR_MSG,INPUT_PROMIT,GROUP_TITLE,USER_CONFIG,SEL_OPTIONS,VIEW_SQL,ORDER_BY) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
		Object params[] = {newBlkModel.getTablemetadata_id(),
				newBlkModel.getFather_tablemetadata_id(),
				newBlkModel.getZh_cn(),
				newBlkModel.getEn_us(),
				newBlkModel.getDescribes(),
				newBlkModel.getObj_path(),
				newBlkModel.getModel_states().getIndex(),
				newBlkModel.getModel_type().getIndex(),
				newBlkModel.getModel_field().getIndex(),
				newBlkModel.getModel_field_len(),
				newBlkModel.getIs_primary_key().getIndex(),
				newBlkModel.getIs_empty().getIndex(),
				newBlkModel.getShow_width(),
				newBlkModel.getIs_hidden().getIndex(),
				newBlkModel.getIs_viewer().getIndex(),
				newBlkModel.getIs_reader().getIndex(),
				newBlkModel.getIs_disabled().getIndex(),
				newBlkModel.getError_msg(),
				newBlkModel.getInput_promit(),
				newBlkModel.getGroup_title(),
				newBlkModel.getUser_config(),
				newBlkModel.getSel_options(),
				newBlkModel.getView_sql(),
				newBlkModel.getOrder_by()
				};
		BatchSqlEntity entity = new BatchSqlEntity();
		entity.setSql(sql.toString());
		entity.setParams(params);
		
		return entity;
	}
	
	/**
	 * 插入字段,直插入业务数据
	 * @param newBlkModel 新字段
	 * @param oldBlkModel 旧字段
	 * @param parentBlkModel 父节点
	 * @return
	 * @throws Exception
	 */
	public BlkTableMetaData updateFieldModel(BlkTableMetaData newBlkModel,BlkTableMetaData parentBlkModel) throws Exception{
		List<BatchSqlEntity> executeSql = new ArrayList<BatchSqlEntity>();

		StringBuffer sql = new StringBuffer("UPDATE BLK_TABLE_METADATA SET FATHER_TABLEMETADATA_ID=?,ZH_CN=?,EN_US=?,DESCRIBES=?,OBJ_PATH=?,MODEL_STATES=?,MODEL_TYPE=?,MODEL_FIELD=?,MODEL_FIELD_LEN=?,IS_PRIMARY_KEY=?,IS_EMPTY=?,SHOW_WIDTH=?,IS_HIDDEN=?,IS_VIEWER=?,IS_READER=?,IS_DISABLED=?,ERROR_MSG=?,INPUT_PROMIT=?,GROUP_TITLE=?,USER_CONFIG=?,SEL_OPTIONS=?,VIEW_SQL=?,ORDER_BY=? WHERE TABLEMETADATA_ID=?");
		Object params[] = {newBlkModel.getFather_tablemetadata_id(),
				newBlkModel.getZh_cn(),
				newBlkModel.getEn_us(),
				newBlkModel.getDescribes(),
				newBlkModel.getObj_path(),
				newBlkModel.getModel_states().getIndex(),
				newBlkModel.getModel_type().getIndex(),
				newBlkModel.getModel_field().getIndex(),
				newBlkModel.getModel_field_len(),
				newBlkModel.getIs_primary_key().getIndex(),
				newBlkModel.getIs_empty().getIndex(),
				newBlkModel.getShow_width(),
				newBlkModel.getIs_hidden().getIndex(),
				newBlkModel.getIs_viewer().getIndex(),
				newBlkModel.getIs_reader().getIndex(),
				newBlkModel.getIs_disabled().getIndex(),
				newBlkModel.getError_msg(),
				newBlkModel.getInput_promit(),
				newBlkModel.getGroup_title(),
				newBlkModel.getUser_config(),
				newBlkModel.getSel_options(),
				newBlkModel.getView_sql(),
				newBlkModel.getOrder_by(),
				newBlkModel.getTablemetadata_id()
				};
		BatchSqlEntity entity = new BatchSqlEntity();
		entity.setSql(sql.toString());
		entity.setParams(params);

		executeSql.add(entity);
		dbHelper.getDBUtils().batch(executeSql);
		//获取
		BlkTableMetaData newNode = dbHelper.getDBUtils().getBean("SELECT * FROM BLK_TABLE_METADATA WHERE TABLEMETADATA_ID='"+newBlkModel.getTablemetadata_id()+"'", BlkTableMetaData.class);
		return newNode;
	}

	/**
	 * 插入字段
	 * @param newBlkModel 新字段
	 * @param oldBlkModel 旧字段
	 * @param parentBlkModel 父节点
	 * @return
	 * @throws Exception
	 */
	@Override
	public BlkTableMetaData updateField(BlkTableMetaData newBlkModel,
			BlkTableMetaData parentBlkModel) throws Exception {
		List<BatchSqlEntity> executeSql = new ArrayList<BatchSqlEntity>();

		StringBuffer sql = new StringBuffer("UPDATE BLK_TABLE_METADATA SET FATHER_TABLEMETADATA_ID=?,ZH_CN=?,EN_US=?,DESCRIBES=?,OBJ_PATH=?,MODEL_STATES=?,MODEL_TYPE=?,MODEL_FIELD=?,MODEL_FIELD_LEN=?,IS_PRIMARY_KEY=?,IS_EMPTY=?,SHOW_WIDTH=?,IS_HIDDEN=?,IS_VIEWER=?,IS_READER=?,IS_DISABLED=?,ERROR_MSG=?,INPUT_PROMIT=?,GROUP_TITLE=?,USER_CONFIG=?,SEL_OPTIONS=?,VIEW_SQL=?,ORDER_BY=? WHERE TABLEMETADATA_ID=?");
		Object params[] = {newBlkModel.getFather_tablemetadata_id(),
				newBlkModel.getZh_cn(),
				newBlkModel.getEn_us(),
				newBlkModel.getDescribes(),
				newBlkModel.getObj_path(),
				newBlkModel.getModel_states().getIndex(),
				newBlkModel.getModel_type().getIndex(),
				newBlkModel.getModel_field().getIndex(),
				newBlkModel.getModel_field_len(),
				newBlkModel.getIs_primary_key().getIndex(),
				newBlkModel.getIs_empty().getIndex(),
				newBlkModel.getShow_width(),
				newBlkModel.getIs_hidden().getIndex(),
				newBlkModel.getIs_viewer().getIndex(),
				newBlkModel.getIs_reader().getIndex(),
				newBlkModel.getIs_disabled().getIndex(),
				newBlkModel.getError_msg(),
				newBlkModel.getInput_promit(),
				newBlkModel.getGroup_title(),
				newBlkModel.getUser_config(),
				newBlkModel.getSel_options(),
				newBlkModel.getView_sql(),
				newBlkModel.getOrder_by(),
				newBlkModel.getTablemetadata_id()
				};
		BatchSqlEntity entity = new BatchSqlEntity();
		entity.setSql(sql.toString());
		entity.setParams(params);

		executeSql.add(entity);
		dbHelper.getDBUtils().batch(executeSql);
		//获取
		BlkTableMetaData newNode = dbHelper.getDBUtils().getBean("SELECT * FROM BLK_TABLE_METADATA WHERE TABLEMETADATA_ID='"+newBlkModel.getTablemetadata_id()+"'", BlkTableMetaData.class);
		return newNode;
	}
	
	/**
	 * 删除字段,只删除业务数据
	 * @param newBlkModel
	 * @param parentBlkModel
	 * @return
	 * @throws Exception
	 */
	public BlkTableMetaData deleteFieldModel(BlkTableMetaData newBlkModel,BlkTableMetaData parentBlkModel)throws Exception{
		return null;
	}

	/**
	 * 删除字段
	 * @param newBlkModel
	 * @param parentBlkModel
	 * @return
	 * @throws Exception
	 */
	public BlkTableMetaData deleteField(BlkTableMetaData newBlkModel,BlkTableMetaData parentBlkModel)throws Exception{
		return null;
	}

	/**
	 * 查询表或者是否是否已经存在
	 * @param model
	 * @return
	 * @throws SQLException
	 */
	@Override
	public boolean queryTableOrViewExists(BlkTableMetaData model) throws SQLException {
		//查询实体表是否已经存在
		StringBuffer sql = new StringBuffer("SELECT NAME FROM SQLITE_MASTER WHERE NAME='").append(model.getEn_us()).append("'");
		List<Map<String,String>> list = dbHelper.getDBUtils().getMapList(sql.toString());
		if(list !=null && !list.isEmpty() && list.size()>0){
			return true;
		}
		//查询BLK_TABLE_METADATA业务模型表，查看信息是否已经存在
		StringBuffer modelSql = new StringBuffer("SELECT * FROM BLK_TABLE_METADATA WHERE EN_US='").append(model.getEn_us()).append("' AND MODEL_TYPE IN ('").append(ModelType.TABLE.getIndex()).append("','").append(ModelType.VIEW.getIndex()).append("')");
		List<Map<String,String>> dataList = dbHelper.getDBUtils().getMapList(modelSql.toString());
		if(dataList !=null && !dataList.isEmpty() && dataList.size()>0){
			return true;
		}
		return false;
	}

	/**
	 * 查询表中的字段是否已经存在,sqllite
	 * 查询指定表中的一条记录，返回的集合中是否包含字段
	 * @param model
	 * @param BlkTableMetaData tableModel 所属表
	 * @return
	 * @throws SQLException
	 */
	public boolean queryTableOrViewFieldExists(BlkTableMetaData model,String tablename)throws SQLException{
		StringBuffer sql = new StringBuffer("SELECT * FROM ").append(tablename).append(" WHERE LIMIT 1");
		List<Map<String,String>> list = dbHelper.getDBUtils().getMapList(sql.toString());
		if(list !=null && !list.isEmpty() && list.size()>0){
			Map<String,String> map = list.get(0);
			if(map.containsKey(model.getEn_us())){
				return true;
			}
		}
		//查询BLK_TABLE_METADATA业务模型表，查看信息是否已经存在
		StringBuffer modelSql = new StringBuffer("SELECT * FROM BLK_TABLE_METADATA WHERE EN_US='").append(model.getEn_us()).append("' AND FATHER_TABLEMETADATA_ID='")
				.append(model.getFather_tablemetadata_id()).append("' AND MODEL_TYPE IN ('").append(ModelType.ATTRIBUTE.getIndex()).append("')");
		List<Map<String,String>> dataList = dbHelper.getDBUtils().getMapList(modelSql.toString());
		if(dataList !=null && !dataList.isEmpty() && dataList.size()>0){
			return true;
		}
		return false;
	}
	
	/**
	 * 判断modelname业务模型名称在节点model的子节点中是否存在
	 * @param model
	 * @param modelname
	 * @return
	 * @throws SQLException
	 */
	public boolean queryModelNodeExists(BlkTableMetaData model,String modelname)throws SQLException{
		String sql = "SELECT * FROM BLK_TABLE_METADATA WHERE FATHER_TABLEMETADATA_ID=(SELECT TABLEMETADATA_ID FROM BLK_TABLE_METADATA WHERE OBJ_PATH='"+model.getObj_path()+"') AND EN_US='"+modelname+"'";
		List<Map<String,String>> list = dbHelper.getDBUtils().getMapList(sql.toString());
		if(list !=null && !list.isEmpty() && list.size()>0){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 查询表字段索引是否存在
	 * @param table_fieldindex_name
	 * @return
	 * @throws SQLException
	 */
	@Override
	public boolean queryIndexExists(String table_fieldindex_name,String tablename) throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT NAME FROM SQLITE_MASTER WHERE TYPE='index' and NAME='").append(table_fieldindex_name).append("'");
		List<Map<String,String>> list = dbHelper.getDBUtils().getMapList(sql.toString());
		if(list !=null && !list.isEmpty() && list.size()>0){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 删除数据库选中的节点及子节点
	 * @param selModel
	 * @param parentModel
	 * @throws Exception
	 */
	@Override
	public void batchModelNode(List<BatchSqlEntity> sqlList) throws Exception {
		dbHelper.getDBUtils().batch(sqlList);
	}

}
